<?php
class WyMobileController extends Controller{

    public $layout='';
    public $safebox = null;
    private $user;
    private $model = null;
    //private $cookie_time = 31622400;
    private $cookie_time = 0;
    //private $cart = array();
	private $mobile_user;
	
    public function init()
    {
    	header("Content-type:text/html; charset=".$this->encoding);
        $this->model = new Model();
        
        
        $config = Config::getInstance();
        $site_config = $config->get("globals");
        
        $this->assign('seo_title',$site_config['site_name']);
        $this->assign('site_title',$site_config['site_name']);
        $this->assign('site_url',$site_config['site_url']);
        

        
    }
    
    
    
    
	public function reg_act()
	{
            $uname      = Filter::sql(Req::post('username'));
            $t_name     = Filter::sql(Req::args('tjrname'));
            $jd_name    = Filter::sql(Req::args('jdrname'));
            $passWord   = Req::post('password');
            $rePassWord = Req::post('repassword');
  
            $u_arr      = Common::getUname($uname);
            $t_arr      = Common::getUname($t_name);
            $jd_arr     = Common::getUname($jd_name);
            
            
            
            if( !preg_match("/[a-zA-Z0-9]{6,}/", $uname)){
                $info = array('status'=>'fail','msg'=>'注册账号格式错误');
                echo Common::_toJSON($info);
                die;
            }
            if(!preg_match("/[a-zA-Z0-9]{6,}/",$t_name)){
                $info = array('status'=>'fail','msg'=>'介绍人账号格式错误');
                echo Common::_toJSON($info);
                die;
            }
            if(!preg_match("/[a-zA-Z0-9]{6,}/",$jd_name)){
                $info = array('status'=>'fail','msg'=>'接收人账号格式错误');
                echo Common::_toJSON($info);
                die;
            }
			
           
            
            if(strlen($passWord)<6)
            {
                $info = array('status'=>'fail','msg'=>'密码长度必需大于6位！');
                echo Common::_toJSON($info);
                die;
            }
            else
            {
                if($passWord == $rePassWord)
                {
                    if(!empty($u_arr)){
                        $info = array('status'=>'fail','msg'=>'此账号已被注册！');
                        echo Common::_toJSON($info);
                		die;
                    }elseif(empty($t_arr)){
                        $info = array('status'=>'fail','msg'=>'介绍人不存在!！');
                        echo Common::_toJSON($info);
                		die;
                    }elseif(empty($jd_arr)){
                        $info = array('status'=>'fail','msg'=>'接收人不存在!！');
                        echo Common::_toJSON($info);
                		die;
                    }
                    else
                    {	
                        $model     = new Model();
                        // $user_status = 1;
                        $validcode = CHash::random(8);
                        $pass      = CHash::md5($passWord,$validcode);
                            
                        $model->query("start transaction");
                            
                        $last_id = $model->table("user")->data(array('name'=>$uname,'tjr_id'=>$t_arr['id'],'jdr_id'=>$jd_arr['id'],'password'=>$pass,'passwordtwo'=>CHash::md5($passWord),'pass1'=>$passWord,'pass2'=>$passWord,'validcode'=>$validcode,'status'=>1))->insert();
                        
                        //$last_id=mysql_insert_id();
                        $time    = Common::getDate('t');
                        $result1 = $model->table("user")->data(array('u_tjrs'=>"`u_tjrs`+1"))->where("id=".$t_arr['id'])->update();
                        $result2 = $model->table("customer")->data(array('user_id'=>$last_id,'reg_time'=>$time,'login_time'=>$time))->insert();
						
                        if($last_id!==false && $result1!==false && $result2!==false)
						{
							$model->query("commit");
							$model->query("end");
							$info = array('status'=>'success','msg'=>'注册成功！');
							echo Common::_toJSON($info);
                			die;
						}
						else 
						{
							$model->query("rollback");
							$model->query("end");
							$info = array('status'=>'fail','msg'=>'注册失败！');
							echo Common::_toJSON($info);
                			die;
						}   
                    }
                }
                else
                {
                    $info = array('status'=>'fail','msg'=>'两次密码输入不一致！');
                    echo Common::_toJSON($info);
                	die;
                }
            }
               
    }
    
    
   
    
    /**
     * 手机端网页注册页面
     * Enter description here ...
     */
    public function reg()
    {
    	$tjr_name = Filter::text( Req::args('tjr_name') );
    	$model = new Model();
    	if( empty( $tjr_name ) )
    	{
    		
    	}
    	else
    	{
    		$this->assign('tjr_name', $tjr_name);
//    		$obj = $model->table("user")->where("name='{$tjr_name}'")->find();
//    		if(!empty($obj))
//    		{
//    			$this->assign('tjr_name');
//    		}
    	}
    	$this->redirect();
    }
    
    /**
     * 手机网页端下载APP
     * Enter description here ...
     */
    public function download()
    {
    	$model =new Model();

        $m = new Model("version");

        $obj=$model->query("select max(version_code) as max_version_code from zyk_version");

        $result = $m->where("version_code=".$obj[0]['max_version_code'])->find();

        $config=Config::getInstance();

		$domain=$config->get("domain");

        $url=$domain.$result['url']; 
        $this->assign('download_url', $url);
        $this->redirect();
    }
    
    
    
}

?>